package com.tg.data.helper;

import android.content.Context;

import com.tange.base.toolkit.FileUtil;
import com.tg.data.BuildConfig;
import com.tg.data.MyObjectBox;
import com.tg.data.bean.DeviceItem;
import com.tg.data.bean.DeviceSettingsInfo;
import com.tg.data.bean.MessageFilter;
import com.tg.data.bean.TrackBean;
import com.tg.data.bean.TrackBean_;
import com.tg.data.http.entity.GetOsdRespBean;
import com.tg.data.http.entity.OsdItem;

import java.io.File;
import java.lang.reflect.Method;
import java.util.List;

import io.objectbox.Box;
import io.objectbox.BoxStore;
import io.objectbox.android.AndroidObjectBrowser;

/**
 * Describe:
 * Created by Rance Rang on 2019/5/10.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public class ObjectBoxHelper {
    private static BoxStore boxStore;

    public static void init(Context context) {

        try {
            boxStoreBuilder(context);
        } catch (Exception e) {
            String path = getAndroidFilesDir(context);
            FileUtil.deleteDirectory(path);
            boxStoreBuilder(context);
        }

        if (BuildConfig.DEBUG) {
            new AndroidObjectBrowser(boxStore).start(context);
        }
    }
    private  static void boxStoreBuilder(Context context){
        String dbName = "app_data";
        boxStore = MyObjectBox.builder()
                .androidContext(context).name(dbName)
                .maxReaders(256)
                .build();
    }
    public static String getAndroidFilesDir(Context context) {
        try {
            Method getFilesDir = context.getClass().getMethod("getFilesDir");
            File filesDir = (File) getFilesDir.invoke(context);
            if (filesDir == null) {
                // Race condition in Android before 4.4: https://issuetracker.google.com/issues/36918154 ?
                System.err.println("getFilesDir() returned null - retrying once...");
                filesDir = (File) getFilesDir.invoke(context);
            }
            if (filesDir != null) {
                return new File(filesDir, "objectbox").getAbsolutePath();
            }

        } catch (Exception ignored) {

        }
        return null;
    }

    public static Box<DeviceItem> getDeviceItem() {
        if (boxStore == null) return null;
        return boxStore.boxFor(DeviceItem.class);
    }

    public static Box<TrackBean> getTrackBeans() {
        if (boxStore == null) return null;
        return boxStore.boxFor(TrackBean.class);
    }


    public static List<TrackBean> getTrackBeans(String uuid){
        Box<TrackBean> box = getTrackBeans();
        if (box!=null){
            return box.query().equal(TrackBean_.uuid, uuid).build().find();
        }
        return  null;
    }


    public static void  saveTrackBeans(List<TrackBean> trackBeans, String uuid){
        Box<TrackBean> box = getTrackBeans();
        if (box!=null){
            List<TrackBean> points = box.query().equal(TrackBean_.uuid, uuid).build().find();
            box.remove(points);

            if (trackBeans != null){
                box.put(trackBeans);
            }
        }
    }

    public static Box<GetOsdRespBean> getGetOsdRespBean() {
        if (boxStore == null) return null;
        return boxStore.boxFor(GetOsdRespBean.class);
    }

    public static Box<MessageFilter> getMessageFilter() {
        if (boxStore == null) return null;
        return boxStore.boxFor(MessageFilter.class);
    }

    public static Box<DeviceSettingsInfo> getDeviceSettingsInfo() {
        if (boxStore == null) return null;
        return boxStore.boxFor(DeviceSettingsInfo.class);
    }

    public static Box<OsdItem> getOsdItem() {
        if (boxStore == null) return null;
        return boxStore.boxFor(OsdItem.class);
    }


    public static void updateSodRespBeanBox(GetOsdRespBean getOsdRespBean){
        Box<GetOsdRespBean> GetOsdRespBeanBox = ObjectBoxHelper.getGetOsdRespBean();
        if (GetOsdRespBeanBox != null){
            GetOsdRespBeanBox.put(getOsdRespBean);
        }
        Box<OsdItem> osdItemBox = ObjectBoxHelper.getOsdItem();
        if (osdItemBox == null){
            return;
        }
        osdItemBox.put(getOsdRespBean.items);
    }
}
